// merchant//merchants/branchStore/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,
    loadMoreHidden: 1,
    loading: false,
    keyword: '',
    showAll: 0,
    currentCity: '',
    parentUid: 0,
    currentCardId: 0,
    lat: 0,
    lng: 0
  },
  changeCity: function () {
    wx.navigateTo({
      url: '../cityList/index?currentCity=' + this.data.currentCity + '&showAll=' + this.data.showAll,
    })
  },

  //所有门店汇总
  allExports: function () {
    wx.redirectTo({
      url: '/merchant/merchants/businReports/index?childType=2&childUid=' + app.globalData.myUserInfo.Uid + '&parentuid=' + this.data.parentUid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cardId = wx.getStorageSync('cardId');
    this.setData({
      currentCardId: cardId
    })
    if (options.showAll) {
      this.setData({
        showAll: options.showAll
      })
    }
    if (options.parentUid) {
      this.data.parentUid = options.parentUid;
    }
    if (options.currentCity) {
      this.setData({
        currentCity: options.currentCity
      })
      this.getGeo(options.currentCity);
    } else {
      this.getLocation();
    }
  },

  getGeo: function (city) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/GetLocation?address=' + city,
      success: function (res) {
        if (res.data.Code == 0) {
          that.data.lat = res.data.Data.location.lat;
          that.data.lng = res.data.Data.location.lng
          that.getBranchList(res.data.Data.location.lat, res.data.Data.location.lng);
        } else {
          that.getBranchList(0, 0);
        }
      }
    })
  },
  // 获取地理位置  
  getLocation: function () {
    let that = this;
    utils.getLocation((location)=>{
      if(location.lat>0){
        that.getCityName(location.lat, location.lng)
        that.getBranchList(location.lat, location.lng);
      } else {
        that.getBranchList(location.lat, location.lng)
      }
    },app);   
  },

  getCityName: function (lat, lng) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/GetRegionByGeo?lat=' + lat + '&lng=' + lng,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            currentCity: res.data.Data.CityName
          })
        }
      }
    })
  },

  getBranchList: function (lat, log) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/GetBranchList?uid=' + that.data.parentUid + '&keyword=' + that.data.keyword + '&longitude=' + log + '&latitude=' + lat + '&page=' + that.data.currentPage,
      success: function (res) {
        if (res.data.Code == 0) {
          that.setData({
            loading: true
          })
          if (res.data.Data.Items && res.data.Data.Items.length > 0) {
            let list = res.data.Data.Items;
            if (that.data.currentPage != 1) {
              let preList = that.data.list;
              list = preList.concat(list);
            }
            that.setData({
              list: list,
              loadMoreHidden: res.data.Data.TotalPages

            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  toCardDetail: function (e) {
    let cardId = e.currentTarget.dataset.id;
    wx.reLaunch({
      url: '/pages/index/index?id=' + cardId
    })
  },

  toExports: function (e) {
    let uid = e.currentTarget.dataset.uid;
    wx.redirectTo({
      url: '/merchant/merchants/businReports/index?childUid=' + uid + '&showAll=0&parentuid=' + this.data.parentUid,
    })
  },
  handleConfirm: function (e) {
    let keyword = e.detail.value;
    this.setData({
      keyword: keyword
    })
    this.getBranchList(this.data.lat, this.data.lng);
  },

  loadMore: function () {
    let that = this;
    if (that.data.loadMoreHidden > 1 && that.data.currentPage < that.data.loadMoreHidden) {
      var currPage = that.data.currentPage;
      that.setData({
        currentPage: currPage + 1
      })
      that.getBranchList(that.data.lat, that.data.lng)
    } else {
      return;
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})